package C046;

import java.util.HashMap;

/**
 * ClassName: C05
 * Package: C046
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/2/8 10:23
 * @Version 1.0
 * @Since 1.0
 */
public class C05 {
    public static void main(String[] args) {
        int[] arr = {8,10,6,16,5};
        System.out.println(longestWPI(arr));
    }

    public static int longestWPI(int[] hours) {
        // 初始化数组
        for (int i = 0; i < hours.length; i++) {
            if(hours[i] > 8) {
                hours[i] = 1;
            } else {
                hours[i] = -1;
            }
        }

        // 求前缀和
        int[] sum = getSumArr(hours);

        // 初始化map
        int res = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < sum.length; i++) {
            if (sum[i] > 0) {
                res = i + 1;
            }
            else if(map.containsKey(sum[i] - 1)) {
                res = Math.max(res, i - map.get(sum[i] - 1));
            }else if(!map.containsKey(sum[i])){
                map.put(sum[i], i);
            }
        }

        return res;
    }

    private static int[] getSumArr(int[] arr) {
        int[] res = new int[arr.length];
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
            res[i] = sum;
        }
        return res;
    }
}
